﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace QLNH_DAL_WS
{
    public class EventDAO
    {
        public List<EventDTO> GetAllEvent()
        {
            RestaurantManagementDataContext data = new RestaurantManagementDataContext();

            var result = from info in data.EVENTs
                         select info;

            List<EventDTO> retVal = new List<EventDTO>();

            foreach (EVENT e in result)
            {
                retVal.Add(new EventDTO(e.ID, e.Description, DateTime.Parse(e.StartDate.ToString()) , DateTime.Parse(e.EndDate.ToString())));
            }

            return retVal;
        }

        public bool AddEvent(EventDTO eventDTO)
        {
            RestaurantManagementDataContext data = new RestaurantManagementDataContext();

            var lastID = from e in data.EVENTs
                         orderby e.ID descending
                         select e.ID;

            var eventCode = (from code in data.CODEs
                             where code.Data.Equals("event")
                             select code).First();

            string eventID = eventCode.Prefix;

            if (lastID.Count() > 0)
            {
                string tmp = lastID.First();
                eventID = Ultilities.GetNextId(tmp, "event");
            }
            else
            {
                for (int i = 0; i < Int32.Parse(eventCode.PostfixLength.ToString()); i++)
                {
                    eventID += "0";
                }
            }

            eventDTO.ID = eventID;
            try
            {
                EVENT e = new EVENT();
                e.ID = eventDTO.ID;
                e.Description = eventDTO.Description;
                e.StartDate = eventDTO.StartDate;
                e.EndDate = eventDTO.EndDate;

                data.EVENTs.InsertOnSubmit(e);
                data.SubmitChanges(System.Data.Linq.ConflictMode.FailOnFirstConflict);
                return true;
            }
            catch
            {
                return false;
            }
        }

        public bool EditEvent(EventDTO eventDTO)
        {
            RestaurantManagementDataContext data = new RestaurantManagementDataContext();

            var result = from e in data.EVENTs
                         where e.ID.Equals(eventDTO.ID)
                         select e;

            if (result.Count() > 0)
            {
                try
                {
                    result.First().Description = eventDTO.Description;
                    result.First().StartDate = eventDTO.StartDate;
                    result.First().EndDate = eventDTO.EndDate;

                    data.SubmitChanges(System.Data.Linq.ConflictMode.FailOnFirstConflict);
                    return true;
                }
                catch
                {
                    return false;
                }
            }
            else
                return false;

            return true;
        }

        public bool RemoveEvent(string id)
        {
            RestaurantManagementDataContext data = new RestaurantManagementDataContext();

            var result = from e in data.EVENTs
                         where e.ID.Equals(id)
                         select e;
            if (result.Count() > 0)
            {
                try
                {
                    data.EVENTs.DeleteOnSubmit(result.First());
                    data.SubmitChanges(System.Data.Linq.ConflictMode.FailOnFirstConflict);
                    return true;
                }
                catch
                {
                    return false;
                }
            }
            else
                return false;
        }
    }
}